﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//

using System.Globalization;

namespace Serotonin.OptionsForms
{
    using System;
    using System.Windows.Forms;
    using Filter;
    using System.Drawing;

    /// <summary>Filter specified properties form.</summary>
    public partial class GrayscaleForm : Form, IOptionsForm
    {
        private readonly Grayscale filter;
        private bool usingCommonAlgorithm;

        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="GrayscaleForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        public GrayscaleForm(Processor processor)
        {
            InitializeComponent();
            filter = AOF.Init(processor, this, gbProcess,
                                new Grayscale()) as Grayscale;
        }

        /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            Bitmap b = filter.Apply(Processor.OriginalImage);
            string s =  Program.L.Text[40] + Program.L.Text[83] + ": " +
                        Program.L.Text[84] + ": R=" + filter.Red + "; " +
                        "G=" + filter.Green + "; " +
                        "B=" + filter.Blue + "; ";
            Processor.Change(b, s); 
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[83]; // Name of filter, e.g. Grayscale
            tabctrlOptions.TabPages[0].Text = Program.L.Text[57]; // Options
            gbTrackbars.Text = Program.L.Text[84];
            gbButtons.Text = Program.L.Text[85]; 
        }

        private void GrayscaleRedTrackBarValueChanged(object sender, EventArgs e)
        {
            filter.Red = GrayscaleRedTrackBar.Value / 10000.0f;
            GrayscaleRedValue.Text = filter.Red.ToString(
                CultureInfo.InvariantCulture);
            if (!usingCommonAlgorithm)
            {
                Processor.ApplyFilterRTP();
            }
        }

        private void GrayscaleGreenTrackBarValueChanged(object sender, EventArgs e)
        {
            filter.Green = GrayscaleGreenTrackBar.Value / 10000.0f;
            GrayscaleGreenValue.Text = filter.Green.ToString(
                CultureInfo.InvariantCulture);
            if (!usingCommonAlgorithm)
            {
                Processor.ApplyFilterRTP();
            }
        }

        private void GrayscaleBlueTrackBarValueChanged(object sender, EventArgs e)
        {
            filter.Blue = GrayscaleBlueTrackBar.Value / 10000.0f;
            GrayscaleBlueValue.Text = filter.Blue.ToString(
                CultureInfo.InvariantCulture);
            if (!usingCommonAlgorithm)
            {
                Processor.ApplyFilterRTP();
            }
        }

        private void BtnBt709Click(object sender, EventArgs e)
        {
            usingCommonAlgorithm = true;
            GrayscaleRedTrackBar.Value = 2125;
            GrayscaleGreenTrackBar.Value = 7154;
            GrayscaleBlueTrackBar.Value = 721;
            usingCommonAlgorithm = false;
            Processor.ApplyFilterRTP();
        }

        private void BtnRmyClick(object sender, EventArgs e)
        {
            usingCommonAlgorithm = true;
            GrayscaleRedTrackBar.Value = 5000;
            GrayscaleGreenTrackBar.Value = 4190;
            GrayscaleBlueTrackBar.Value = 810;
            usingCommonAlgorithm = false;
            Processor.ApplyFilterRTP();
        }

        private void BtnYClick(object sender, EventArgs e)
        {
            usingCommonAlgorithm = true;
            GrayscaleRedTrackBar.Value = 2999;
            GrayscaleGreenTrackBar.Value = 5870;
            GrayscaleBlueTrackBar.Value = 1140;
            usingCommonAlgorithm = false;
            Processor.ApplyFilterRTP();
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {
            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }
    }
}
